package com.aaa.sys.service.impl;

import com.aaa.sys.entity.Menu;
import com.aaa.sys.mapper.MenuMapper;
import com.aaa.sys.service.MenuService;
import com.aaa.sys.vo.MenuTree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author ykq
 * @since 2020-11-25
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<String> queryPermissionByUserId(Long userId) {
        List<String> menus=menuMapper.queryPermissionByUserId(userId);
        return menus;
    }

    @Override
    public List<MenuTree> findMenuTreeByUserId(Long userId){
        List<MenuTree> firstmenu=new ArrayList<>();
        List<Menu> list=this.baseMapper.findMenuTreeByUserId(userId);
        //查找所有的一级菜单
        for(Menu first:list){
            if(StringUtils.equals(first.getPcode(),"0")){ //表示一级菜单
                MenuTree menuTree=new MenuTree();
                BeanUtils.copyProperties(first,menuTree); //把first中的属性名称与MenuTree中属性名称对应的复制过去
                firstmenu.add(menuTree);
            }
        }
        //查询所有的二级菜单
        for(Menu second:list){
            for(MenuTree first:firstmenu){
                if(StringUtils.equals(second.getPcode(),first.getCode())){ //表示该菜单属于该一级菜单下的子菜单
                    MenuTree secondMenu=new MenuTree();
                    BeanUtils.copyProperties(second,secondMenu);
                    first.getChildren().add(secondMenu);
                }
            }
        }
        return firstmenu;
    }

    @Override
    public List<Menu> queryMenuTreeByCon(String name) {
        List<Menu> root=new ArrayList<>();
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(name)){
             wrapper.like("name",name);
        }
        List<Menu> menus = this.menuMapper.selectList(wrapper);
        for(Menu menu:menus){
             if(StringUtils.equals(menu.getPcode(),"0")){
                  root.add(menu);
             }
        }

        for(Menu parent:root){
              parent.setChildren(findChild(parent.getCode(),menus));
        }
        return root;
    }

    @Override
    public List<Integer> queryExpandMenuKey(Long roleId) {
         return this.menuMapper.queryExpandMenuKey(roleId);
    }


    private List<Menu> findChild(String code, List<Menu> menus) {
        List<Menu> children=new ArrayList<>();
        for(Menu menu:menus){
              if(StringUtils.equals(menu.getPcode(),code)){
                  children.add(menu);
              }
        }

        for(Menu child:children){
            child.setChildren(findChild(child.getCode(),menus));
        }
        return children;
    }




}
